package cn.jhc.sevlet;//获取学号列表

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.Student;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.impl.StudentDaoImpl;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GetSnoServlet
 */
@WebServlet("/user/teacher/getSnoServlet.do")
public class GetSnoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetSnoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		StudentDao studentDao = new StudentDaoImpl();
		
		List<Student> students = studentDao.list();
		
		JSONArray json = new JSONArray();
		
		for (Student student : students) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("sno", student.getSno());
			json.add(jsonObject);
		}
		PrintWriter out = response.getWriter();
		out.print(json.toString());
		out.flush();
	}

}
